*===============================================================================
*
*					WORKER BELIEFS ABOUT OUTSIDE OPTIONS
*		(c)	Simon Jaeger, Christopher Roth, Nina Roussille, Benjamin Schoefer
*							  2023 December 5
*						   	   SOEP-IAB Data 
*
*===============================================================================


********************************************************************************
*								Figure III 		 							   *
********************************************************************************

cap log close 
log using ${log}/FigureIIIab.log, replace  

* read in data: Full time EUE population 
use "$data/FTp_u__finaldata.dta", clear	

* y-axis 
global ytitle "Belief About Wage Change if Moving to Outside Option" 
	
	
	
*** final variable preparation
* winsorizing 'coworker wage changes' variable
cap drop actual_delta_ln_mover_change 
winsor mean_delta_ln_wage_mover_2015, gen(actual_delta_ln_mover_change) p(0.02) 

* winsorizing empirical bayes variable 
cap drop ebayes_delta_ln_mover_change
winsor ebayes_delta_ln_wage_mover_2015, gen(ebayes_delta_ln_mover_change) p(0.02)

* winsorizing split sample IV variable
cap drop actual_delta_ln_mover_change_r0 
winsor mean_delta_ln_wage_mover_r0_2015, gen(actual_delta_ln_mover_change_r0) p(0.02) 
cap drop actual_delta_ln_mover_change_r1 
winsor mean_delta_ln_wage_mover_r1_2015, gen(actual_delta_ln_mover_change_r1) p(0.02) 

* erase help files 
cap erase "$data/binscdata.do"
cap erase "$data/binscdata.csv"
cap erase "$data/binscdata_EB.do"
cap erase "$data/binscdata_EB.csv"
																
*************		
* Figure 3 a 
*************										
					
*** creating the main exhibit plots with coworker wage changes as the benchmark

* obtaining coefficient and SE from regression of beliefs on raw mover wage changes
reg ln_own_change_w actual_delta_ln_mover_change, cluster(pid)
local N_obs = e(N)
local beta = string(round(_b[actual_delta_ln_mover_change],0.001),"%4.3f")
local se = string(round(_se[actual_delta_ln_mover_change],0.001),"%4.3f")
local constant = _b[_cons]
local se_constant = _se[_cons]
				
* coefficient and SE from split-sample IV regression
ivregress 2sls ln_own_change_w (actual_delta_ln_mover_change_r1 = actual_delta_ln_mover_change_r0),  cluster(pid) 
local N_obs_IV = e(N)
local beta_IV = string(round(_b[actual_delta_ln_mover_change_r1],0.001),"%4.3f")
local se_IV = string(round(_se[actual_delta_ln_mover_change_r1],0.001),"%4.3f")

* coefficient and SE from empirical bayes regression
reg ln_own_change_w ebayes_delta_ln_mover_change, cluster(pid)
local N_obs_EB = e(N)
local beta_EB = string(round(_b[ebayes_delta_ln_mover_change],0.001),"%4.3f")
local se_EB = string(round(_se[ebayes_delta_ln_mover_change],0.001),"%4.3f")
local constant_EB = string(round(_b[_cons],0.001),"%4.3f")
local extraEB "Empirical Bayes Slope: `beta_EB' (SE `se_EB')"

* empirical bayes line							
binscatter ln_own_change_w ebayes_delta_ln_mover_change ebayes_delta_ln_mover_change, savedata("$data/binscdata_EB")
preserve					
		clear
		do "$data/binscdata_EB"
		gen mergevar = _n
		rename ln_own_change_w ln_own_change_w_eb
		save "$data/ebplots_temp.dta", replace
restore
					
* raw data line
binscatter ln_own_change_w actual_delta_ln_mover_change actual_delta_ln_mover_change, savedata("$data/binscdata")
					
* constructing the plot
clear 							
do "$data/binscdata" // loading raw data line
gen mergevar = _n // merging on EB line
merge 1:1 mergevar using "$data/ebplots_temp.dta", keep(master match) nogen

* setting axis ranges
local max = 1
local min = -0.6

local slopemin = `min'
local slopemax = `max'

foreach rangevar in ln_own_change_w actual_delta_ln_mover_change {
	quietly su `rangevar'
	if r(min)<`slopemin' local slopemin = r(min)
	if r(max)>`slopemax' local slopemax = r(max)
}

twoway connect ln_own_change_w actual_delta_ln_mover_change if ln_own_change_w==69, lc(navy) mc(navy) msymbol(circle) ///
	|| connect ln_own_change_w actual_delta_ln_mover_change if ln_own_change_w==69, lc(orange) mc(orange) lpattern(dash_dot) msymbol(triangle_hollow) ///
	|| function y = x, range(`min' `max') color(black%50) ///
	|| function y = (`beta_IV'*x)+`constant', range(`slopemin' `slopemax') color(maroon) lpattern(longdash) ///
	|| function y = (`beta_EB'*x)+`constant', range(`slopemin' `slopemax') color(navy) ///
	|| scatter ln_own_change_w_eb ebayes_delta_ln_mover_change, mcolor(navy) msymbol(circle) ///
	|| scatter ln_own_change_w actual_delta_ln_mover_change, mcolor(orange) msymbol(triangle_hollow)  ///
	|| function y = (`beta'*x)+`constant', range(`slopemin' `slopemax') color(orange) lpattern(dash_dot) ///
	, legend(order(1 "Empirical Bayes Slope: `beta_EB' (SE `se_EB')" 4 "Split-Sample IV Slope:  `beta_IV' (SE `se_IV')" 2 "Unadjusted Slope:         `beta' (SE `se')") cols(1) ring(0) size(small) position(11) region(lwidth(none))) legend(size(medsmall)) ///
	xscale(range(`min' `max')) yscale(range(`min' `max')) ylabel(#10,grid) xlabel(#10,grid) ///
	xtitle("Objective Benchmark: Mover Wage Changes") ytitle("${ytitle}") 
	addplot: scatteri 0.425 0.35 "Accurate Beliefs: Slope = 1", msymbol(i) mlabpos(0) mlabangle(35) mlabcolor(black) legend(order(1 "Empirical Bayes Slope: `beta_EB' (SE `se_EB')" 4 "Split-Sample IV Slope:  `beta_IV' (SE `se_IV')" 2 "Unadjusted Slope:         `beta' (SE `se')") cols(1) ring(0) size(small) position(11) region(lwidth(none))) legend(size(medsmall))

graph export ${log}/FigureIIIa.pdf, replace

		
					
									
*************		
* Figure 3 b 
*************

cap erase "$data/binscdata.do"
cap erase "$data/binscdata.csv"		

* read in data: Full time EUE population 
use "$data/FTp_u__finaldata.dta", clear									
								
* calculating coefficient and SE
reg ln_own_change_w p_delta_ln_wage_mover, cluster(pid)
local N_obs = e(N)
local beta = string(round(_b[p_delta_ln_wage_mover],0.001),"%4.3f")
local se = string(round(_se[p_delta_ln_wage_mover],0.001),"%4.3f")
local constant = string(round(_b[_cons],0.001),"%4.3f")
cap local se_constant = _se[_cons]
									
* setting up binscatter plot
binscatter ln_own_change_w p_delta_ln_wage_mover p_delta_ln_wage_mover, savedata("$data/binscdata")
							
clear 

do "$data/binscdata"

local min = -0.6
local max = 0.6

local slopemin = `min'
local slopemax = 1

foreach rangevar in ln_own_change_w p_delta_ln_wage_mover {
	quietly su `rangevar'
	if r(min)<`slopemin' local slopemin = r(min)
	if r(max)>`slopemax' local slopemax = r(max)
}

twoway connect ln_own_change_w p_delta_ln_wage_mover if ln_own_change_w==69, lc(navy) mc(navy) ///
	|| function y = x, range(`slopemin' `slopemax') color(black%50) ///
	|| scatter ln_own_change_w p_delta_ln_wage_mover, mcolor(navy) msymbol(circle) ///
	|| function y = (`beta'*x)+`constant', range(`slopemin' `slopemax') color(maroon) ///
	,  xscale(range(`min' `max')) yscale(range(`min' `max')) ylabel(#10,grid) xlabel(#10,grid) ///
	xtitle("Objective Benchmark: ML Prediction") ytitle("${ytitle}") ///
	legend(order(- "Slope: `beta' (SE `se')") cols(1) ring(0) size(medsmall) position(11) region(lwidth(none))) 
	addplot: scatteri 0.475 0.4 "Accurate Beliefs: Slope = 1", msymbol(i) mlabpos(0) mlabcolor(black) mlabangle(35)	legend(order(- "Slope: `beta' (SE `se')") cols(1) ring(0) size(medsmall) position(11) region(lwidth(none)))

graph export ${log}/FigureIIIb.pdf, replace

log close 
clear